<template>
  <div class="app-container">
    <AttractionInfo v-show="showAttrDetail" />
    <AddAttraction v-show="isAdd" :is-add.sync="isAdd" />
    <!-- 查询条件框 -->
    <el-card v-show="!showAttrDetail&&!isAdd" class="box-card">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item v-model="form.name" label="景区名称">
          <el-input v-model="form.name" placeholder="景区名称" />
        </el-form-item>
        <el-form-item label="景区类别">
          <el-select v-model="form.attrCateId" placeholder="请选择景区类别">
            <el-option v-for="item in attrTypes" :key="item.cateId" :label="item.cateName" :value="item.cateId" />
          </el-select>
        </el-form-item>
        <el-form-item label="地区">
          <el-select v-model="form.areaId" placeholder="请选择地区">
            <template v-for="item in areas">
              <el-option :key="item.areaId" :label="item.areaName" :value="item.areaId" />
            </template>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="addAttr">添加新景区</el-button>
        </el-form-item>
      </el-form>
      <el-divider />
      <!-- 景区信息列表 -->
      <el-table :data="attractions" style="width: 100%" stripe class="table">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="attrId" label="编号" width="50" />
        <el-table-column prop="bannerUrl" label="封面" width="100">
          <template slot-scope="scope">
            <el-image style="width: 80px; height: 50px" :src="scope.row.bannerUrl" :preview-src-list="[scope.row.bannerUrl]" lazy fit>
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="景区名称" />
        <el-table-column prop="areaId" label="地区">
          <template slot-scope="scope">
            {{ getAreaName(scope.row.areaId, areas) }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="详细地址" />
        <el-table-column prop="attrCateId" label="景区类别">
          <template slot-scope="scope">
            {{ getTypeName(scope.row.attrCateId, attrTypes) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-row>
              <el-button type="primary" icon="el-icon-more-outline" circle @click="isShowMore(scope.row.attrId)" />
              <el-button type="primary" icon="el-icon-edit" circle @click="editAttraction(scope.row.attrId)" />
              <el-button type="danger" icon="el-icon-delete" circle @click="delAttr(scope.row.attrId)" />
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :page-size="form.pageSize" :pager-count="11" layout="prev, pager, next" :total="total" class="bottom" @current-change="handleCurrentChange" />
    </el-card>
    <!-- 详情信息框 -->
    <el-dialog
      title="景区详情"
      :visible.sync="centerDialogVisible"
      width="50%"
      center
      destroy-on-close
    >
      <AttractionDialog />
    </el-dialog>
  </div>
</template>

<script>
import { getAttrs, delAttraction, searchAttrList } from '@/api/travel'
import AttractionInfo from '@/components/AttractionInfo/index.vue'
import AttractionDialog from '@/components/AttractionDialog'
import AddAttraction from '@/components/AttractionInfo/AddAttraction.vue'
import { mapGetters } from 'vuex'
import { getAreaName, getTypeName } from '@/utils/attractionUtil'

export default {
  name: 'Travel',
  components: {
    AttractionInfo,
    AttractionDialog,
    AddAttraction
  },
  inject: ['reload'],
  data() {
    return {
      attractions: [],
      pageNum: 1,
      pageSize: 5,
      pageSum: 0,
      total: 0,
      // areas: [],
      // attrTypes: [],
      isShowDetail: false,
      getAreaName,
      getTypeName,
      form: {
        pageNum: 1,
        pageSize: 5
      },
      centerDialogVisible: false,
      isAdd: false
    }
  },
  computed: {
    ...mapGetters([
      'attrTypes',
      'areas',
      'showAttrDetail',
      'attrId'
    ])
  },
  watch: {
    showAttrDetail: function(newVal, pldVal) {
      this.getAllAttrs()
    }
  },
  created() {
    this.getAllAttrs()
    this.getAreas()
    this.getAttrTypes()
  },
  methods: {
    async getAllAttrs() {
      const res = await getAttrs(this.pageNum, this.pageSize)
      if (res.code === 200) {
        const { attractions, total } = res.data
        this.attractions = attractions
        this.total = total
      } else {
        this.$message.error(res.message)
      }
    },
    getAreas() {
      this.$store.dispatch('attraction/getAreas')
    },
    async getAttrTypes() {
      this.$store.dispatch('attraction/getAttrTypes')
    },

    delAttr(id) {
      this.$confirm('是否确定删除该景区信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delAttraction(id).then(res => {
          if (res.code === 200) {
            this.$message.success(res.message)
            // 更新页面数据
            this.reload()
          } else {
            this.$message.error(res.message)
          }
        }).catch(() => {
          this.$message.error('删除失败')
        })
      })
    },

    isShowMore(attrId) {
      this.$store.commit('attraction/SET_ATTR_ID', attrId)
      this.centerDialogVisible = true
    },
    editAttraction(attrId) {
      this.$store.commit('attraction/SET_SHOW_ATTR_DETAIL', true)
      this.$store.commit('attraction/SET_ATTR_ID', attrId)
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    async search() {
      console.log(this.form)
      const res = await searchAttrList(this.form)
      console.log(res)
      if (res.code === 200) {
        console.log(res)
        const { attractions, total } = res.data
        this.attractions = attractions
        this.total = total
      } else {
        this.$message.error('查询错误')
      }
    },
    addAttr() {
      this.isAdd = true
      // this.$store.commit('attraction/SET_SHOW_ATTR_DETAIL', true)
    },
    handleCurrentChange(val) {
      this.form.pageNum = val
      this.search().then(res => {
        this.form.pageNum = 1
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  width: 100%;

  .box-card {
    width: 100%;
    height: 100%;

    .table {
      width: 100%;
      height: 470px;
    }

    .bottom {
      margin-top: 10px;
      display: flex;
      justify-content: center;
    }

  }
}
</style>
